const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataType: 'assign',
    isLoading: true,
    page: 1,
    no_more: false,
    rand:1, 
    
    shop_cate:0,//分类
    inputValue:'',//商店名称
    coupon_type:0,//类别
    people_num:0,//参与人数
    time:0,//参与时间

    shoppingMall:[],
    shoppingMallName:'',
    shoppingMallId:'',
    // 下拉菜单
    first: '选择商城',
    second: '筛选',
    // thirds: '房型',
    // fours: '筛选',
    // _num: 0,
    // _res: 0,
    dist:[],
    shopping_mall_city:'',
    shopping_mall_district:'',
    // 筛选
    shop_cate_res: [],
    coupon_type_res: [{ id: 10, name: '满减券' }, { id: 20, name: '折扣券' }],
    people_num_res: [{ id: 2, name: '2人' }, { id: 3, name: '3人' }, { id: 4, name: '4人' }, { id: 5, name: '5人' }, { id: 6, name: '6人' }, { id: 7, name: '7人' }, { id: 8, name: '8人' }, { id: 9, name: '9人' }, { id: 10, name: '10人' }],
    time_res: [{ id: 1, name: '无时间限制' }, { id: 2, name: '当天' }, { id: 3, name: '7天以内' }, { id: 4, name: '7天以后' }],
    
    one: 0,
    two: 0,
    third: 0,
    four: 0,
    // five: 0,
    // six: 0,
    // seven: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let shopping_mall_city = wx.getStorageSync('shopping_mall_city');//城市名称
    let shopping_mall_district = wx.getStorageSync('shopping_mall_district');//区名称
    let user_address_id = wx.getStorageSync('user_address_id');//商城id
    let user_address_cont = wx.getStorageSync('user_address_cont');//商城名称
    //缓存中有
    if (shopping_mall_city) {
      this.setData({
        shopping_mall_city: shopping_mall_city
      })
    }
    if (shopping_mall_district) {
      this.setData({
        shopping_mall_district: shopping_mall_district
      })
    }
    // if (user_address_id){
    //   this.setData({
    //     shoppingMallId: user_address_id
    //   })
    // }
    if (user_address_cont){
      this.setData({
        shoppingMallName: user_address_cont
      })
    }
    
    //根目录路径
    this.setData({
      app_root: App.root
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
    //console.log(this.data.shoppingMallId);

    let team = this.data.list;
    //获取分类
    this.getShopCate();
    //获取 区信息
    this.getDistrict();
    // 获取 商城地址
    this.getShoppingMall();
    // 获取信息列表
    if(!team){
      this.getTeamList();
    }
  },

  onReady: function () {
    //页面渲染完成 第三运行
    //console.log('onReady');
  },

  getShopCate:function(){
    let _this = this;
    App._get('shop/getShopCate', {
    }, function (result) {
      //console.log(result);
      _this.setData({
        shop_cate_res: result.data.shopCate
      });
    });
  },

  //获取区信息
  getDistrict:function(){
    let _this = this;
    let user_address_id =  this.data.shoppingMallId
    let shopping_mall_city = this.data.shopping_mall_city;
    let shopping_mall_district = this.data.shopping_mall_district;
    
    App._get('Shopping_mall/getDistrict', {
      shopping_mall_id: user_address_id, 
      shopping_mall_city: shopping_mall_city,
      shopping_mall_district: shopping_mall_district
    }, function (result) {
      _this.setData({
        dist: result.data.list
      });
    });
  },
  //获取商城地址
  getShoppingMall:function(){
    let _this = this;
    //let user_address_id = wx.getStorageSync('user_address_id');
    let user_address_id = this.data.shoppingMallId ? this.data.shoppingMallId : wx.getStorageSync('user_address_id') ;
    App._get('Shopping_mall/getShoppingMall', {
      shopping_mall_id: user_address_id
    }, function (result) {
      //console.log(result);
      _this.setData({
        shoppingMall: result.data.list
      });
    });
  },

  /**
   * 获取我的团队列表
   */
  getTeamList: function(isNextPage, page) {
    
    let _this = this;
    //console.log(_this.data.dataType);
    App._get('shop/index', {
      page: page || 1, 
      rand: _this.data.rand,
      shoppingMallId: _this.data.shoppingMallId,
      inputValue: _this.data.inputValue,
      shop_cate: _this.data.shop_cate,
    }, function(result) {
       console.log(result);
       
      // 创建页面数据
      let tang = _this.createData(result.data, isNextPage);
      _this.setData(tang);
    });
  },

  /**
   * 创建页面数据
   */
  createData: function(data, isNextPage) {
    data['isLoading'] = false;
    // 列表数据
    let dataList = this.data.list;
    if (isNextPage == true && (typeof dataList !== 'undefined')) {
      data.list.data = dataList.data.concat(data.list.data);
    }
    // 设置swiper的高度
    this.setSwiperHeight();
    return data;
  },

  /**
   * 下拉到底加载数据
   */
  triggerDownLoad: function() {
    // 已经是最后一页
    if (this.data.page >= this.data.list.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    this.getTeamList(true, ++this.data.page);
  },
  //随机刷新
  rand:function(){
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 2
    });
    this.getTeamList();
  },
  //刷新页面
  newPage:function(){
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 1,
      currentTab:3
    });
    this.getTeamList();

  },
  //返回正常排序
  gotoSort:function(){
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 1
    });
    this.getTeamList();
  },
  /**
   * 设置swiper的高度
   */
  setSwiperHeight: function(isTap) {
    // 获取系统信息(拿到屏幕宽度)
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = isTap ? Math.floor(rpx * 82) : 0, // tap高度
      peopleHeight = Math.floor(rpx * 65), // people高度
      tang = Math.floor(rpx * 170), 
      swiperHeight = systemInfo.windowHeight - tapHeight - peopleHeight-tang; // swiper高度
    
    this.setData({
      swiperHeight
    });
  },


  //下拉js
  isShow: true,
  currentTab: 0,

  // 下拉切换
  hideNav: function () {
    this.setData({
      displays: "none"
    })
  },
  // 区域
  tabNav: function (e) {
    let dis = this.data.displays;
    if (dis == 'block' && this.data.currentTab === e.target.dataset.current){
      this.setData({
        displays: "none"
      })
    }else{
      this.setData({
        displays: "block"
      })
    }
    
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {

      var showMode = e.target.dataset.current == 0;

      this.setData({
        currentTab: e.target.dataset.current,
        isShow: showMode
      })
      
    }
  },
  // 下拉切换中的切换
  // 选择 区
  selected: function (e) {
    let _this = this;
    let dist_id = e.currentTarget.dataset.id;
    //let user_address_id = wx.getStorageSync('user_address_id');//商城id
    let user_address_id = this.data.shoppingMallId
    App._get('Shopping_mall/clickDist', {
      dist_id: dist_id,
      shopping_mall_id: user_address_id
    }, function (result) {
      _this.setData({
        dist: result.data.dist_res
      });
      _this.setData({
        shoppingMall: result.data.mall
      });
    });


  },
  
  // 商城选择
  clickShoppingMall: function (e) {
    let shoppingMall = this.data.shoppingMall;
    for (let i = 0; i < shoppingMall.length; i++) {
      shoppingMall[i]['selected'] = 'add_city';
      if (e.currentTarget.dataset.id == shoppingMall[i]['id']){
        shoppingMall[i]['selected'] = 'add_adress';
      }
    };
    this.setData({
      shoppingMall: shoppingMall,
      shoppingMallId: e.currentTarget.dataset.shopping_mall_id,
      shoppingMallName: e.currentTarget.dataset.name
    });
    this.getTeamList();//显示 社交对象
    this.setData({
      displays: "none"
    })
  },

  //显示全部
  allShopping: function () {
    let shoppingMall = this.data.shoppingMall;
    for (let i = 0; i < shoppingMall.length; i++) {
      shoppingMall[i]['selected'] = 'add_city';
    };
    this.setData({
      shoppingMall: shoppingMall,
      shoppingMallId: ''
    });
    this.getTeamList();//显示 社交对象
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 1,
      displays: "none"
    });
  },
  
  clickMum: function (e) {
    this.setData({
      _mum: e.target.dataset.num
    })
    this.setData({
      displays: "none"
    })
    var text = this.data.name
  },
 
  clickCum: function (e) {
    this.setData({
      _cum: e.target.dataset.num
    })
    this.setData({
      displays: "none"
    })
    var text = this.data.name
    console.log(text)
  },
  
  // 售价
  clickNum: function (e) {
    //console.log(e.target.dataset.num)
    this.setData({
      _num: e.target.dataset.num
    })
    this.setData({
      second: e.target.dataset.name
    })
    this.setData({
      displays: "none"
    })
    var text = this.data.name
    //console.log(text)
  },
  
  // 房型
  clickHouse: function (e) {
    this.setData({
      _res: e.target.dataset.num
    })
    this.setData({
      thirds: e.target.dataset.name
    })
    this.setData({
      displays: "none"
    })
  },
  

  // 筛选
  chose_shop_cate:function(e){
    this.setData({
      shop_cate: e.currentTarget.dataset.id,
      one: e.currentTarget.dataset.id
    })
  },
  //输入店名
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  //确认 
  queren: function (e) {
    
    let _this = this;
    this.getTeamList();
    this.setData({
      isLoading: true,
      page: 1,
      no_more: false,
      displays: "none",
    })
    
  },

  //清除
  qingchu: function () {
    this.setData({
      isLoading: true,
      page: 1,
      no_more: false,

      shop_cate: 0,//分类
      inputValue:'',
      coupon_type: 0,//类别
      people_num: 0,//参与人数
      time: 0,//参与时间
      one: 0,
      two: 0,
      third: 0,
      four:0,
      displays: "none"
    })
    this.getTeamList();
  },

  // choseTxtColor: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   //console.log(e.currentTarget.dataset.id)
  //   this.setData({
  //     one: id
  //   })
  // },
  // chaoxiang: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     two: id
  //   })
  // },
  // louceng: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     third: id
  //   })
  // },
  // zhuangxiu: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     four: id
  //   })
  // },
  // leibei: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     five: id
  //   })
  // },
  // tese: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     six: id
  //   })
  // },
  // paixu: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     seven: id
  //   })
  // },

  /**
   * 切换标签
   */
  bindHeaderTap: function (e) {

    this.setData({
      dataType: e.currentTarget.dataset.type
    })

    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 1,
      currentTab: 3
    });
    this.getTeamList();

    // let urls = {
    //   receive: '/pages/coupon/index?type=receive',
    //   assign: '/pages/order/index?type=assign',
    //   init: '/pages/doing/index?type=init',
    // };
    // // 转跳指定的页面
    // wx.navigateTo({
    //   url: urls[e.currentTarget.dataset.type]
    // })
  },

  //跳转 用户详情页
  goToDetail: function (e) {
    let user_id = e.currentTarget.dataset.user_id;
    wx.navigateTo({
      url: '/pages/social/detail?user_id=' + user_id
    })
  },

})